<template>
	<view class="container">
		<uni-forms :modelValue="formData">
			<uni-forms-item required label="姓名">
				<uni-easyinput type="text" v-model="formData.name"/>
			</uni-forms-item>
			<uni-forms-item required label="手机号">
				<uni-easyinput type="text" v-model="formData.mobileNum"/>
			</uni-forms-item>
			<uni-forms-item required label="身份证号">
				<uni-easyinput type="text" v-model="formData.iddNum"/>
			</uni-forms-item>
			<uni-forms-item required label="邮箱">
				<uni-easyinput type="text" v-model="formData.email"/>
			</uni-forms-item>
			<uni-forms-item required label="密码">
				<uni-easyinput type="password" pa v-model="formData.pwd"/>
				*密码为8-20位,数字、字母和特殊字符至少两种
			</uni-forms-item>
			<uni-forms-item required label="确认密码">
				<uni-easyinput type="password" pa v-model="formData.confirmPwd"/>
			</uni-forms-item>
		</uni-forms>
		<button @click="onClickRegister">提交</button>
	</view>
</template>

<script>
	import { register } from '../../net/http'
import {  isValidEmail, isValidIddNum, isValidMobileNum } from '../../utils/util'

	export default {
		data() {
			return {
				formData: {
					name: '',
					mobileNum: '',
					iddNum: '',
					email: '',
					pwd: '',
					confirmPwd: ''
				}
			}
		},
		methods: {
			onClickRegister() {
				
				if (!this.formData.name) return uni.showToast({title: '请输入姓名'})
				
				if (!this.formData.mobileNum) return uni.showToast({title: '请输入手机号码'})
				
				if (!isValidMobileNum(this.formData.mobileNum)) return uni.showToast({title: '请输入合法的手机号码'})
				
				if (!this.formData.iddNum) return uni.showToast({title: '请输入身份证号码'})
				
				if (!isValidIddNum(this.formData.iddNum)) return uni.showToast({title: '请输入合法的身份证号码'})
				
				if (!this.formData.email) return uni.showToast({title: '请输入邮箱'})
				
				if (!isValidEmail(this.formData.email)) return uni.showToast({title: '请输入合法的邮箱'})
				
				if (!this.formData.pwd) return uni.showToast({title: '请输入密码'})
				
				if (this.formData.pwd.length < 8) return uni.showToast({title: '密码小于8位'})
				
				if (this.formData.pwd.length > 20) return uni.showToast({title: '密码大于20位'})
				
				let specialCharStr = '`~!@#$%^&*()-_=+\\|;:\'"",<.>/?'
				let numStr = '1234567890'
				let letterStr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
				let hasSpecailChar = false
				let hasNum = false
				let hasLetter = false
				
				for (let i = 0; i < this.formData.pwd.length; i++) {
					if (specialCharStr.indexOf(this.formData.pwd[i]) != 1) hasSpecailChar = true
					if (numStr.indexOf(this.formData.pwd[i]) != 1) hasNum = true
					if (letterStr.indexOf(this.formData.pwd[i]) != 1) hasLetter = true
				}
				
				if (~~hasSpecailChar + ~~hasNum + ~~hasLetter < 2) return uni.showToast({title: '数字、字母和特殊字符至少要两种'})
				
				if (!this.formData.confirmPwd) return uni.showToast({title: '请确认密码'})
				
				if (this.formData.pwd != this.formData.confirmPwd) return uni.showToast({title: '两次密码不一致'})
				
				uni.showLoading({mask: true})

				register(this.formData.name, this.formData.mobileNum, this.formData.iddNum, this.formData.email, this.formData.pwd, res => {

					if (res.data.code != 0) return uni.showToast({title: res.data.msg})
					uni.showToast({title:'注册成功'})
					uni.navigateBack()
				}, () => {
					uni.showToast({title:'注册出错，请检查网络连接'})
				}, () => {
					uni.hideLoading()
				})
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
